<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="专业" prop="specialized">
                <el-input v-model="queryParams.specialized" placeholder="请输入专业" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="宿舍号" prop="dormId">
                <el-input v-model="queryParams.dormId" placeholder="请选择宿舍号" clearable size="small" style="width: 240px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" size="mini" @click="handleAdd"
                    v-hasRole="['student']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" size="mini" @click="handleDelete2"
                    v-hasRole="['student']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" size="mini" @click="handleExport"
                    v-hasRole="['student']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 表格数据 -->
        <el-table v-loading="loading" :data="recordList" stripe border table-layout="fixed">
            <el-table-column label="姓名" prop="name" :show-overflow-tooltip="true" sortable />
            <el-table-column label="手机号" prop="phone" :show-overflow-tooltip="true" sortable />
            <el-table-column label="专业" prop="specialized" sortable />
            <el-table-column label="性别" align="center" sortable>
                <template #default="scope">
                    <el-tag :type="scope.row.sex == 1 ? 'success' : 'warning'">{{ scope.row.sex
                        == 1 ? ' 男 ' : ' 女 ' }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="宿舍" align="center" prop="dormName" sortable />
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script setup name="studentInfo">
import { ElNotification } from 'element-plus'
import { getUserInfoApi } from '@/api/student'
import { exportExcel } from '@/utils/excelExport'

const { proxy } = getCurrentInstance();

const recordList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        phone: undefined,
        specialized: undefined,
        dormId: undefined
    }
});

const { queryParams, form } = toRefs(data);

/** 查询我的填写访客列表 */
const getList = () => {
    loading.value = true;
    getUserInfoApi(queryParams.value).then(res => {
        if (res.code == 200) {
            recordList.value = res.data.vo;
            total.value = res.data.total;
            loading.value = false;
        } else {
            recordList.value = [];
            total.value = 0;
            loading.value = false;
        }
    })
}
getList();
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}
/** 导出按钮操作 */
function handleExport() {
    let arr = []
    recordList.value.forEach(item => {
        let out = {}
        out.name = item.name;
        out.phone = item.phone;
        out.specialized = item.specialized;
        if (item.sex == '1') {
            out.sex = '男'
        } else if (item.sex == '0') {
            out.sex = '女'
        }
        out.dormName = item.dormName;
        arr.push(out)
    })
    const titleArr = ['姓名', '手机号', '专业', '性别', '宿舍']  //表头中文名
    exportExcel(arr, '学生信息', titleArr, '所在楼宇学生信息')
}
</script>
